import { ApiAddTask } from "@@/api/wk/index";
import useBalanceStore, { StoreCoinItem } from "@@/store/useBalanceStore";
import useTabPageStore from "@@/store/useTabPageStore";
import { add, chain, divide } from "mathjs";

export function computeAddTaskPlayInfo(
  form: ApiAddTask.Req
): ComputeAddTaskPlayInfoRes {
  const balabceStore = useBalanceStore();
  const WKC = balabceStore.getCoinByCode("WKC");
  const tabPageStore = useTabPageStore();
  /**佣金 */
  const commission = Number(form.commission);
  /**本金 */
  const capital = Number(form.capital);
  const task_setting = tabPageStore.homeData?.task_setting;
  /**服务费比率 */
  const serveRatio = Number(task_setting?.platform_service_fee_scale);
  /**服务费 */
  const servePrice = chain(serveRatio)
    .multiply(0.01)
    .multiply(add(commission, capital))
    .done();

  /**任务保证金比率 */
  const capitalRatio = Number(task_setting?.fluctuation_insurance_premium);
  const price = Number(tabPageStore.homeData?.wkc.price);
  /**任务保证金 */
  const capitalPrice = chain(capitalRatio)
    .multiply(0.01)
    .multiply(chain(capital).add(commission).add(servePrice).done())
    .done();
  const usdtAmount = chain(commission)
    .add(servePrice)
    .add(capital)
    .add(capitalPrice)
    .done();
  const amount = divide(usdtAmount, price);
  return {
    servePrice,
    address: tabPageStore.homeData?.address.safe_address!,
    amount,
    coin: WKC!,
    usdtAmount,
    capitalPrice,
    form,
  };
}

export interface ComputeAddTaskPlayInfoRes {
  servePrice: number;
  address: string;
  amount: number;
  coin: StoreCoinItem;
  usdtAmount: number;
  capitalPrice: number;
  form: ApiAddTask.Req;
}
